package framework.injection
{
import flash.errors.IllegalOperationError;
import flash.utils.Dictionary;
import flash.utils.describeType;

/**
 * DefaultInjectManager
 * @author jliang
 * 
 */	
public class DefaultInjectManager implements IInjectManager
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function DefaultInjectManager(enforce:SingletonEnforce)
	{
		if (!enforce)
		{
			throw new IllegalOperationError("DefaultInjectManager is singleton class.");
		}
		instance = this;
	}
	
	private static var instance:DefaultInjectManager;
	
	public static function getInstace():DefaultInjectManager
	{
		return instance ? instance : new DefaultInjectManager(new SingletonEnforce());
	}
	
	private static var injections:Dictionary = new Dictionary(true);
	
	public function inject(component:IInjectable):void
	{
		var doc:XML = describeType(component);
		/**
		 * 
		 * do nothing here
		 * 
		 */	
		setInjection(component, doc.variable);
		setInjection(component, doc.accessor);		
	}
	
	private function setInjection(component:*, list:*):void
	{
		for each (var varItem:* in list)
		{
			if (varItem["metadata"]) 
			{
				var md:XMLList = varItem.metadata.(@name == "Inject");
				if (md.@name != "Inject")
				{
					continue;
				}
				var varName:String = String(varItem.@name);
				if (injections[varName])
				{
					component[varName] = injections[varName];
				}
			}
		}
	}
	
	public function addInjection(key:String, value:*):void
	{
		injections[key] = value;
	}
	
	public function removeInjection(key:String):void
	{
		delete injections[key];
	}
}
}

class SingletonEnforce
{
	
}